package playn.game.core;

import static playn.core.PlayN.*;

import java.util.ArrayList;

import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.Keyboard;
import playn.core.Surface;
import playn.core.SurfaceLayer;
import playn.core.Keyboard.Event;
import playn.core.Keyboard.TypedEvent;
import playn.core.Pointer;
import playn.game.core.Game;
import playn.game.core.transitions.Fade;
import tiled.simple.core.Map;
import tiled.simple.core.MapObject;
import tiled.simple.core.ObjectGroup;
import tiled.simple.core.Renderer;
import tiled.simple.core.TMXObjectsProvider;

public class State extends Pointer.Adapter implements Keyboard.Listener,TMXObjectsProvider, Renderer{

	protected ArrayList<Button> buttons=new ArrayList<Button>();
	public GroupLayer groupLayer;

	protected SurfaceLayer staticLayer;
	
	protected String name="";

	private final static int STATUS_DISABLED=0;
	private final static int STATUS_FADE_IN =1;
	private final static int STATUS_FADE_OUT=3;
	
	private int status=STATUS_DISABLED;
	
	public Transition transition=null;

	public State(GroupLayer layer){
		groupLayer=layer;
	}
	
	
	public void start(){
		System.out.println(name+" start");
		transition=new Fade();
		transition.addIn(groupLayer);
		transition.addOut(groupLayer);

		int w=map.getWidth()*map.getTileWidth();
		int h=map.getHeight()*map.getTileHeight();
		staticLayer=graphics().createSurfaceLayer(w,h);

		groupLayer.add(staticLayer);
		
		map.render(this);
	}
	
	public State next=null;

	public void end(){
		System.out.println(name+" end");
		groupLayer.clear();
	}
	
	protected Map map;
	
	public void enable(){
		System.out.println(name+" enable");
		buttons.add(Game.soundManager.soundButton);
		buttons.add(Game.soundManager.musicButton);
		
		ObjectGroup buttons=map.getObjectGroup("buttons");
		if(buttons!=null){
			buttons.loadObjects(this);
		}
		
		keyboard().setListener(this);
		pointer().setListener(this);
		
	}
	
	public void disable(){
		System.out.println(name+" disable");
		buttons=new ArrayList<Button>();
	}
	
	public void show(){
		System.out.println(name+" show");
		status=STATUS_FADE_IN;
	}
	
	public void hide(){
		System.out.println(name+" hide");
		status=STATUS_FADE_OUT;
	}

	@Override
	public void onKeyDown(Event event) {
	}

	@Override
	public void onKeyTyped(TypedEvent event) {
	}

	@Override
	public void onKeyUp(Event event) {
	}
	
	
	public void onPress(Button button){

		if(button.id().equalsIgnoreCase(SoundManager.SOUND_BUTTON_ID)){
			System.out.println("sound");
			Game.soundManager.sound();
		}

		if(button.id().equalsIgnoreCase(SoundManager.MUSIC_BUTTON_ID)){
			System.out.println("music");
			Game.soundManager.music();
		}
		
		System.out.println("button "+button.id());

	}

	@Override
    public void onPointerEnd(Pointer.Event event) {
    	for(Button button:buttons){
    		if(button.clicked((int)event.x(), (int)event.y())){
    			onPress(button);
    		}
    	}
    }
	
	@Override
	public void object(MapObject mapObject) {
		buttons.add(new Button(mapObject.getX(),mapObject.getY(),mapObject.getWidth(),mapObject.getHeight(),mapObject.getProperty("id")));
	}
	
	public void putImage(String source, int sx, int sy, int sw, int sh, int dx,int dy, int dw, int dh) {
		Image image=null;
		
		if(!Game.images.containsKey(source)){
			System.out.println("you have to load "+source+" image from loader using imagesCache");
		} else {
			image=Game.images.get(source);
		}
		
		Surface surface = staticLayer.surface();
		surface.drawImage(image, dx, dy, dw, dh, sx, sy, sw, sh);
	}

	
	public void update(float delta) {
		if((transition!=null)&&(status!=STATUS_DISABLED)){
			switch(status){
			case STATUS_FADE_IN : {	
										if(transition.in(delta)){
											status=STATUS_DISABLED;
											enable();
										};
										break;
								  }
			case STATUS_FADE_OUT: {
										if(transition.out(delta)){
											status=STATUS_DISABLED;
											end();
											Game.menu.currentState=next;
											Game.menu.currentState.start();

											Game.menu.currentState.show();
										};
										break;
								  }
			default: break;
			}
			//groupLayer.setVisible(true);
		}

	}

}
